package com.sanyou.service.impl;

import com.sanyou.enums.IsOk;
import com.sanyou.mapper.MaintainHistoryMapper;
import com.sanyou.pojo.MaintainHistory;
import com.sanyou.service.MaintainHistoryService;
import com.sanyou.utils.BeanUtils;
import com.sanyou.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.List;

@Service
public class MaintainHistoryServiceImpl implements MaintainHistoryService {
    @Autowired
    MaintainHistoryMapper maintainHistoryMapper;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void addMaintainHistory(MaintainHistory maintainHistory) throws Exception {
        /**
         * 为维修记录生成id
         */
        if(maintainHistory.getId() == null){
            maintainHistory.setId(SnowFlake.getNextId());
        }

        /**
         * 插入维修记录
         */
        maintainHistoryMapper.insert(maintainHistory);
    }

    @Override
    public List<MaintainHistory> queryMaintainHistoryByProductCode(String productCode) {
        return this.queryMaintainHistory(null,null,productCode,null,null,null);
    }

    @Override
    public List<MaintainHistory> queryMaintainHistoryByProductCode(String productCode, Integer maintainType) {
        return this.queryMaintainHistory(null,null,productCode,maintainType,null,null);
    }

    @Override
    public List<MaintainHistory> queryMaintainHistoryByProductId(String productId) {
        return this.queryMaintainHistory(null,productId,null,null,null,null);
    }

    @Override
    public List<MaintainHistory> queryMaintainHistoryByProductId(String productId, Integer maintainType) {
        return this.queryMaintainHistory(null,productId,null,maintainType,null,null);
    }

    @Override
    public MaintainHistory queryMaintainHistoryById(Long id) {
        List<MaintainHistory> historyList = this.queryMaintainHistory(id,null,null,null,null,null);

        if(historyList == null || historyList.isEmpty()){
            return null;
        }
        return historyList.get(0);
    }

    @Override
    public List<MaintainHistory> queryMaintainHistory(Long id, String productId, String productCode,
                                                      Integer maintainType, String maintainerId,
                                                      String maintainerName) {
        Example example = new Example(MaintainHistory.class);
        Example.Criteria criteria = example.createCriteria();
        /**
         * 查询条件设置
         */
        if(id!=null){
            criteria.andEqualTo("id",id);
        }

        if(productId!=null){
            criteria.andEqualTo("productId",productId);
        }

        if(productCode!=null){
            criteria.andEqualTo("productCode",productCode);
        }

        if(maintainType!=null){
            criteria.andEqualTo("maintainType",maintainType);
        }

        if(maintainerId!=null){
            criteria.andEqualTo("maintainerId",maintainerId);
        }

        if(maintainerName!=null){
            criteria.andEqualTo("maintainerName",maintainerName);
        }

        example.setOrderByClause("maintain_time desc");

        return maintainHistoryMapper.selectByExample(example);
    }
}
